package app.imttf.online.model;

import app.imttf.online.adfextensions.CustomApplicationModuleImpl;
import app.imttf.online.model.common.SecurityAM;
import app.imttf.online.model.view.admin.access.UserAccessRolesViewImpl;

import app.imttf.online.model.view.admin.access.UserPasswordImpl;
import app.imttf.online.utils.PasswordEncoder;

import java.util.HashMap;
import java.util.Map;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.server.ViewObjectImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Mon Jan 06 14:52:59 ICT 2014
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class SecurityAMImpl extends CustomApplicationModuleImpl implements SecurityAM {
    /**
     * This is the default constructor (do not remove).
     */
    public SecurityAMImpl() {
    }

    /**
     * Function to find user row by login and password
     * @return map with user data and list of privileges
     */
    public Map authenticateUser(String userLogin, String userPassword) {
        
        Map userData = new HashMap();
        HashMap userAccess = new HashMap();
        
        ViewObjectImpl user = this.getUserAccessView1();
        user.setMaxFetchSize(1);
        user.setNamedWhereClauseParam("userLogin", userLogin);
        /* -- ENCRYPTED */
        user.setNamedWhereClauseParam("userPassword",PasswordEncoder.encodePassword(userLogin + ":" + userPassword));
        
        /* -- NOT ENCRYPTED
        user.setNamedWhereClauseParam("userPassword",userPassword);
        */
        user.executeQuery();

        if (user.getEstimatedRowCount() > 0) {

            Row userRow = user.first();
            String fullName = (String)userRow.getAttribute("FullName");
            String userNameLogin = (String)userRow.getAttribute("UserName");
            String companyName = (String)userRow.getAttribute("Company");
            String kodeDcName = (String)userRow.getAttribute("BranchName");
            
            String userType = (String)userRow.getAttribute("UserType");
                        
            userData.put("FullName", fullName);
            userData.put("UserName", userNameLogin);
            userData.put("Password", userPassword);            
            userData.put("CompanyName", companyName);
            userData.put("KodeDcName", kodeDcName);
            userData.put("UserType", userType);
            setToSession("UserId", userLogin); // Save Login ID to BC session
            
            //Retrive User Roles
            UserAccessRolesViewImpl groupOfRoles;
            groupOfRoles = this.getUserAccessRolesView1();
            groupOfRoles.setNamedWhereClauseParam("userNameLogin", userNameLogin);
            groupOfRoles.executeQuery();

            while (groupOfRoles.hasNext()) {
                Row privsRow = groupOfRoles.next();
                String roleName = (String)privsRow.getAttribute("Role");
                String userName = (String)privsRow.getAttribute("UserName");
                userAccess.put(roleName, userName);
            }
            
            userData.put("UserAccess", userAccess);
        }

        return userData;
    }

    /**
     * Container's getter for UserAccessRolesView1.
     * @return UserAccessRolesView1
     */
    public UserAccessRolesViewImpl getUserAccessRolesView1() {
        return (UserAccessRolesViewImpl)findViewObject("UserAccessRolesView1");
    }

    /**
     * Container's getter for UserAccessView1.
     * @return UserAccessView1
     */
    public ViewObjectImpl getUserAccessView1() {
        return (ViewObjectImpl)findViewObject("UserAccessView1");
    }
    
    /*
     * Change Password for User Page 
     */
    public void changePassword(String newPassword) {
        String userId = (String)getFromSession("UserId");
        ViewObjectImpl user = getUserPassword1();
        ViewCriteria uc = user.createViewCriteria();
        ViewCriteriaRow userVcr = uc.createViewCriteriaRow();
        userVcr.setAttribute("UserName", "=" + userId);
        uc.addElement(userVcr);
        user.applyViewCriteria(uc);
        user.executeQuery();

        Row userRow = user.first();
        userRow.setAttribute("Password", newPassword);
        getDBTransaction().commit();
    }

    /**
     * Container's getter for UserPassword1.
     * @return UserPassword1
     */
    public UserPasswordImpl getUserPassword1() {
        return (UserPasswordImpl)findViewObject("UserPassword1");
    }
}
